﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _4Gewinnt
{
    /// <summary>
    /// Selects the engines for a tournament. 
    /// </summary>
    public partial class SelectEnginesTournament_GUI : Form
    {
        /// <summary>
        /// The engines to participate
        /// </summary>
        public List<Type> SelectedEngines;
        /// <summary>
        /// The number of matches each engine plays against each other
        /// </summary>
        public int nRounds;
        /// <summary>
        /// Show the moves in the GUI (slow)
        /// </summary>
        public bool showBoardInGUI;

        /// <summary>
        /// Initializes the dialog
        /// </summary>
        public SelectEnginesTournament_GUI()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Shows the initialization window
        /// </summary>
        /// <param name="availableEngines">The types of available engines</param>
        public void startSelection(Type[] availableEngines)
        {
            listBoxAvailableEngines.DataSource = availableEngines;
            listBoxAvailableEngines.DisplayMember = "Name";

            this.ShowDialog();
        }


        /// <summary>
        /// Read selected engines and nRounds from GUI and close Form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (listBoxTournamentEngines.Items.Count != 0)
            {
                //Read selected engines from GUI
                SelectedEngines = new List<Type>();
                foreach (var item in listBoxTournamentEngines.Items)
                    SelectedEngines.Add((Type)item);

                //Read number of rounds from GUI
                nRounds = Convert.ToInt32(numericUpDown1.Value);

                //Show GUI while tournament?
                showBoardInGUI = checkBoxGUI.Checked;

                this.Close();
            }
        }

        /// <summary>
        /// Select all engines
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSelectAll_Click(object sender, EventArgs e)
        {
            foreach (var item in listBoxAvailableEngines.Items)
                listBoxTournamentEngines.Items.Add((Type)item);
            updateNMatches();
        }

        /// <summary>
        /// Select one engine
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSelectOne_Click(object sender, EventArgs e)
        {
            listBoxTournamentEngines.Items.Add((Type)listBoxAvailableEngines.SelectedItem);
            updateNMatches();
        }

        /// <summary>
        /// Deselect one engine
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDeselectOne_Click(object sender, EventArgs e)
        {
            listBoxTournamentEngines.Items.RemoveAt(listBoxTournamentEngines.SelectedIndex);
            updateNMatches();
        }

        /// <summary>
        /// Deselect all engines
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDeselectAll_Click(object sender, EventArgs e)
        {
            listBoxTournamentEngines.Items.Clear();
            updateNMatches();
        }

        /// <summary>
        /// Update the number of matches played in the tournament when changing the number of rounds
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            updateNMatches();
        }


        /// <summary>
        /// Shows the number of matches played in the tournament
        /// </summary>
        private void updateNMatches()
        {
            int nContestants = listBoxTournamentEngines.Items.Count;
            int nRounds = Convert.ToInt32(numericUpDown1.Value);

            labelNMatches.Text = (2 * (nContestants * (nContestants - 1)) / 2 * nRounds).ToString();
        }

    }
}
